package lab4;


import com.alibaba.fastjson.JSONObject;

import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Scanner;


public class Client {
    private String clientAccount;
    private RSAPrivateKey rsaPrivateKey;//发布签名时的密钥
    private RSAPublicKey rsaPublicKey;
    Client(String clientAccount) throws NoSuchAlgorithmException {
        this.clientAccount = clientAccount;
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        this.rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        this.rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    }

    public String getClientAccount(){
        return clientAccount;
    }


    public RSAPublicKey getRsaPublicKey(){
        return this.rsaPublicKey;
    }

    public byte[] setSignature(String message) throws NoSuchAlgorithmException, InvalidKeySpecException,
            InvalidKeyException, SignatureException {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(message.getBytes());
        return signature.sign();
    }

    public void withdraw(Integer money, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account",getClientAccount());
        jsonObject.put("money",money);
        jsonObject.put("password", setSignature(password));
        EBank.Instance().withdrawHelp(jsonObject);
    }

    public void deposit(Integer money, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account",getClientAccount());
        jsonObject.put("money",money);
        jsonObject.put("password", setSignature(password));
        EBank.Instance().depositHelp(jsonObject);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException,
            InvalidKeySpecException, SignatureException, InvalidKeyException {
        Client client = new Client("10086");
        Scanner sc = new Scanner(System.in);
        System.out.println("注册：请输入密码");
        String eBankPassword = sc.next();
        //注册
        EBank.Instance().register(client.getClientAccount(), eBankPassword, client.getRsaPublicKey());
        System.out.println("存取款：请输入您的密码");
        String inputPassword = sc.next();
        client.deposit(1000, inputPassword);
        client.withdraw(200, inputPassword);
        client.withdraw(900, inputPassword);
    }
}
